package com.cl.master.controller;

import com.cl.master.common.Result;
import com.cl.master.domain.User;
import com.cl.master.service.impl.UserServiceImpl;
import lombok.val;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author xc
 * @Date 2023/5/19 10:41
 * @Version 1.0
 */
@RestController//=@Controller+@ResponseBody表示该类是springboot的一个controller，且返回的数据为JSON格式
// @Component
@RequestMapping("sewage")
public class UserController {
    @Resource
    private UserServiceImpl userService;

    //查询数据
    @RequestMapping("getForm")
    public Result getForm(HttpServletRequest request){
        String loginAdmin = (String) request.getSession().getAttribute("loginAdmin");
        Map<String,Object> data = userService.getForm(loginAdmin);
        Result result = Result.success(data);
        return result;
    }

    @PostMapping("saveForm")
    public Result submitForm(@RequestBody User user, HttpServletRequest request) {
        val loginAdmin = request.getSession().getAttribute("loginAdmin");
        user.setUsername((String) loginAdmin);
        userService.saveUser(user);
        Result result = Result.success("成功保存");
        return  result;
    }
}
